package com.sugar.psych.assessment.module.business.controller.admin.userleavemessage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.userleavemessage.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.userleavemessage.UserLeaveMessageDO;
import com.sugar.psych.assessment.module.business.service.userleavemessage.UserLeaveMessageService;

@Tag(name = "管理后台 - 用户留言")
@RestController
@RequestMapping("/business/user-leave-message")
@Validated
public class UserLeaveMessageController {

    @Resource
    private UserLeaveMessageService userLeaveMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建用户留言")
    @PreAuthorize("@ss.hasPermission('business:user-leave-message:create')")
    public CommonResult<Long> createUserLeaveMessage(@Valid @RequestBody UserLeaveMessageSaveReqVO createReqVO) {
        return success(userLeaveMessageService.createUserLeaveMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户留言")
    @PreAuthorize("@ss.hasPermission('business:user-leave-message:update')")
    public CommonResult<Boolean> updateUserLeaveMessage(@Valid @RequestBody UserLeaveMessageSaveReqVO updateReqVO) {
        userLeaveMessageService.updateUserLeaveMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户留言")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-leave-message:delete')")
    public CommonResult<Boolean> deleteUserLeaveMessage(@RequestParam("id") Long id) {
        userLeaveMessageService.deleteUserLeaveMessage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户留言")
                @PreAuthorize("@ss.hasPermission('business:user-leave-message:delete')")
    public CommonResult<Boolean> deleteUserLeaveMessageList(@RequestParam("ids") List<Long> ids) {
        userLeaveMessageService.deleteUserLeaveMessageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户留言")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-leave-message:query')")
    public CommonResult<UserLeaveMessageRespVO> getUserLeaveMessage(@RequestParam("id") Long id) {
        UserLeaveMessageDO userLeaveMessage = userLeaveMessageService.getUserLeaveMessage(id);
        return success(BeanUtils.toBean(userLeaveMessage, UserLeaveMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户留言分页")
    @PreAuthorize("@ss.hasPermission('business:user-leave-message:query')")
    public CommonResult<PageResult<UserLeaveMessageRespVO>> getUserLeaveMessagePage(@Valid UserLeaveMessagePageReqVO pageReqVO) {
        return success(userLeaveMessageService.getUserLeaveMessagePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户留言 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-leave-message:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserLeaveMessageExcel(@Valid UserLeaveMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "用户留言.xls", "数据", UserLeaveMessageRespVO.class,
                userLeaveMessageService.getUserLeaveMessagePage(pageReqVO).getList());
    }

}